﻿using System;

namespace ch05.binary.interpreter
{
    public struct Vm
    {
        internal OperandStack OperandStack;
        internal Module Module;

        public Vm(OperandStack op, Module mo)
        {
            this.OperandStack = op;
            this.Module = mo;
        }

        internal void ExecCode(int idx)
        {
            var code = Module.CodeSec[idx];
            foreach (var instr in code.Expr)
            {
                ExecInstr(instr);
            }
        }

        private void ExecInstr(Instruction instr)
        {
            //fmt.Printf("%s %v\n", instr.GetOpname(), instr.Args)
            var op = Instr.GetInstrFn()[instr.Opcode];
            op(ref this, instr.Args);
        }
    }
}